<!DOCTYPE HTML>
<title>SVGLengthList, insertItemBefore()</title>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<script src="resources/SVGLengthList-helper.js"></script>
<svg width="200" height="200">
    <text x="500 1000 1500" y="50">   ABC  </text>
</svg>
<script>
test(function() {
  // This is a test of the SVGLengthList.insertItemBefore() API.

  var svg = document.querySelector("svg");
  var list = document.querySelector("text").x.baseVal;

  // Create three SVGLength objects, with values=50,100,150");
  var newLength1 = svg.createSVGLength();
  newLength1.value = 50;
  assert_equals(newLength1.value, 50);

  var newLength2 = svg.createSVGLength();
  newLength2.value = 100;
  assert_equals(newLength2.value, 100);

  var newLength3 = svg.createSVGLength();
  newLength3.value = 150;
  assert_equals(newLength3.value, 150);

  assert_not_equals(newLength1.value, newLength2.value);
  assert_not_equals(newLength2.value, newLength3.value);
  assert_not_equals(newLength3.value, newLength1.value);
  assert_equals(newLength1.value, newLength2.value - 50);
  assert_equals(newLength2.value + 50, newLength3.value);

  // Check initial list state");
  assert_equals(list.numberOfItems, 3);
  assert_equals(list.getItem(0).value, 500);
  assert_equals(list.getItem(1).value, 1000);
  assert_equals(list.getItem(2).value, 1500);
  assert_throws_dom("IndexSizeError", function() { list.getItem(3); });

  // Spec: If the index is greater than or equal to numberOfItems, then the new item is appended to the end of the list.
  // Insert item 'newLength1' at the end of the list, by using a large index.
  assert_equals(list.insertItemBefore(newLength1, 1000).value, newLength1.value);
  assert_list(list, [500, 1000, 1500, 50]);

  // Storing getItem(0/1/2/3) in local variables.
  var item0 = list.getItem(0);
  var item1 = list.getItem(1);
  var item2 = list.getItem(2);
  var item3 = list.getItem(3);

  // Spec: If the index is equal to 0, then the new item is inserted at the front of the list.
  // Insert item 'newLength2' at the front of the list, by using index=0".
  assert_equals(list.insertItemBefore(newLength2, 0).value, newLength2.value);
  assert_list(list, [100, 500, 1000, 1500, 50]);

  // Assure that previously saved wrappers still show the old values.
  assert_equals(item0.value, 500);
  assert_equals(item1.value, 1000);
  assert_equals(item2.value, 1500);
  assert_equals(item3.value, 50);

  // Spec: The index of the item before which the new item is to be inserted.

  // Insert item 'newLength3' at position=2, between '500' and '1000'.
  assert_equals(list.insertItemBefore(newLength3, 2).value, newLength3.value);
  assert_list(list, [100, 500, 150, 1000, 1500, 50]);

  // Spec: If newItem is already in a list, then a new object is created with the same values as newItem and this item is inserted into the list.
  // Otherwise, newItem itself is inserted into the list.

  // Insert item 'newLength3' at position=1, between '100' and '500', should not remove it from the old position=2 afterwards.");
  assert_equals(list.insertItemBefore(newLength3, 1).value, newLength3.value);
  assert_equals(list.numberOfItems, 7);
  assert_list(list, [100, 150, 500, 150, 1000, 1500, 50]);

  // Insert item 'newLength1' at position=0, before '100', should not remove it from the old position=6 afterwards.
  assert_equals(list.insertItemBefore(newLength1, 0).value, newLength1.value);
  assert_list(list, [50, 100, 150, 500, 150, 1000, 1500, 50]);
});
</script>